// import AppLayout from '@/views/AppLayout/index.vue'
import Home from '@/views/Home/index.vue'
// import Goods from '@/views/Goods/index.vue'
import goodsControls from '@/views/Goods/index.vue'
import goodList from '@/views/Goods/GoodsList'
import goodsCategory from '@/views/Goods/GoodsGetgory'
import Params from '@/views/Params/index.vue'
import voiceClass from '@/views/voiceClass/index.vue'
import OrderControl from '@/views/orderControl/index.vue'
import orderList from '@/views/orderControl/orderList/index.vue'
import orderBack from '@/views/orderControl/orderBack/index.vue'
import addProduct from "@/views/Goods/addProduct.vue"
import contractPdf from '@/views/orderControl/contract/index.vue'
import map from '@/views/map/map.vue'

// 路由表, 希望也能用这个数据去渲染菜单
// 如果有name, 就认为是一级路由组件, 就需要做菜单
// 如果有parent属性
const orderControl = {

    path: '/orderControl',
    name: 'orderControl',
    title: '订单管理',
    redirect: "/orderControl/orderList",
    component: OrderControl,
    meta: {
        title: '订单管理',
        icon: 'icon-dingdanliebiao',
    },
}

const GoodsControl = {


    path: '/GoodsControl',
    name: 'goodsControl',
    title: '产品管理',
    redirect: "/GoodsControl/goodsList",
    component: goodsControls,
    meta: {
        title: '产品管理',
        icon: 'icon-dingdanliebiao',
    },
}
const routes = [
    // {
    //     path: '/', redirect: { name: 'Home' }
    // },

    // {
    //     path: '/Home',
    //     title: '首页',
    //     name: 'Home',
    //     component: Home,
    //     meta: {
    //         title: '首页',
    //         icon: 'icon-shouye'
    //     }
    // },
    {
        path: '/GoodsControl',
        name: 'goodsControl',
        title: '产品管理',
        redirect: "/GoodsControl/goodsList",
        component: goodsControls,
        meta: {
            title: '产品管理',
        },
        children: [
            {
                path: '/GoodsControl/goodsList',
                name: 'GoodsControlgoodsList',
                parent: GoodsControl,
                parentPath: 'GoodsControl',
                title: '产品列表',
                component: goodList,
                meta: {
                    title: '产品列表',
                    icon: 'icon-shangpinguanli'
                }
            },
            {
                path: '/GoodsControl/goodsCategory',
                name: 'GoodsControlgoodsCategory',
                parent: GoodsControl,
                parentPath: 'GoodsControl',
                title: '产品分类',
                component: goodsCategory,
                meta: {
                    title: '产品分类',
                    icon: 'icon-shangpinguanli'
                }

            },
            {
                path: '/GoodsControl/addProduct',
                name: 'GoodsControladdProduct',
                parent: GoodsControl,
                parentPath: 'GoodsControl',
                component: addProduct,
                title: '添加商品',
                meta: {
                    activeMenu: 'goodsList',
                    title: '添加商品'
                },
            }

        ]
    },
    {
        path: '/orderControl',
        name: 'orderControl',
        title: '订单管理',
        redirect: "/orderControl/orderList",
        component: OrderControl,
        meta: {
            title: '订单管理',
        },
        children: [
            {
                path: '/orderControl/orderList',
                name: 'orderControlorderList',
                parent: orderControl,
                parentPath: '/orderControl',
                title: '订单列表',
                component: orderList,
                meta: {
                    title: '订单列表',
                    icon: 'icon-dingdanliebiao'
                }
            },
            {
                path: '/orderControl/orderBack',
                name: 'orderControlorderBack',
                parent: orderControl,
                parentPath: '/orderControl',
                title: '汇总清单',
                component: orderBack,
                meta: {
                    title: '汇总清单',
                    icon: 'icon-tuihuoguanli'
                }
            },
            {
                path: '/orderControl/contractPdf',
                name: 'orderControlcontractPdf',
                parent: orderControl,
                parentPath: '/orderControl',
                title: '订单审核',
                component: contractPdf,
                meta: {
                    title: '订单审核',
                    icon: 'icon-tuihuoguanli'
                }
            },
        ]
    },
    // {
    //     path: '/GoodsControl/addProduct',
    //     name: 'addProduct',
    //     parent: GoodsControl,
    //     parentPath: 'GoodsControl',
    //     component: addProduct,
    //     meta: {
    //         activeMenu: 'goodsList',
    //         title: '添加商品'
    //     }
    //     // title: '产品分类',
    //     // component: goodsCategory,
    //     // meta: {
    //     //     icon: 'icon-shangpinguanli'
    //     // }

    // },
    // {
    //     path: '/map',
    //     name: 'map',
    //     // parent: bannerParent,
    //     title: '地图',
    //     component: map,
    //     meta: {
    //         icon: 'icon-guigecanshu'
    //     }
    // },
    // {
    //     path: '/params',
    //     name: 'Params',
    //     // parent: bannerParent,
    //     title: '规格参数',
    //     component: Params,
    //     meta: {
    //         icon: 'icon-guigecanshu'
    //     }
    // },
    {
        path: '/voiceClass',
        name: 'voiceClass',
        // parent: bannerParent,
        title: '广告分类',
        component: voiceClass,
        meta: {
            icon: 'icon-guanggaofenleiguanli'
        }
    },

]


// 将路由表routes, 转化成菜单表menus
export const formatMenusFromRoutes = (routes) => {
    const result = []
    // for (let i = 0; i < routes.length; i++) {
    //     // 如果没有name, 就不是一级路由组件, 就不需要做菜单
    //     if (!routes[i].title) continue
    //     // 如果没有parent属性, 就认为是一级路由组件, 就需要做菜单, 直接放入结果中
    //     if (!routes[i].parent) result.push({ ...routes[i], id: routes[i].name })
    //     // 如果有parent属性, 就认为是二级路由组件, 需要创建一个父级菜单, 并将自己放入父级菜单的children中
    //     // 如果父级菜单已经存在, 就将自己放入父级菜单的children中
    //     // 如果父级菜单不存在, 就创建一个父级菜单, 并将自己放入父级菜单的children中
    //     else {
    //         // 找爸爸
    //         const parent = result.find(item => item.name === routes[i].parent.name)
    //         parent ? parent.children.push({ ...routes[i], id: routes[i].name }) : result.push({
    //             id: routes[i].parent.name,
    //             name: routes[i].parent.name,
    //             title: routes[i].parent.title,
    //             meta: routes[i].parent.meta,
    //             children: [routes[i]]
    //         })
    //     }
    // }
    for (let i = 0; i < routes.length; i++) {
        // console.log(routes[i], '这个是理由的值');
        if (!routes[i].title) continue
        if (!routes[i].children) result.push({ ...routes[i], id: routes[i].name })

        // 来到这里就说明了第二个问题  这里的全部都有父级路由
        else {
            routes[i].children.forEach((item) => {
                // console.log(item, '这里处理父级的数据');
                const parent = result.find((item1) => item1.name == item.parent.name)
                // parent ? parent = [] : result.push({
                //     id: item.parent.name,
                //     name: item.parent.name,
                //     title: item.parent.meta,
                //     children: [item]
                // })

                if (!!parent) {
                    parent.children.push({
                        ...item, id: item.name,
                    })
                } else {
                    result.push({
                        id: item.parent.name,
                        name: item.parent.name,
                        title: item.parent.title,
                        meta: item.parent.meta,
                        path: item.parent.path,
                        redirect: item.parent.redirect,
                        component: item.parent.component,
                        children: [item]
                    })
                }

            })
            // console.log(result, '第二步处理的路由');
            //  const parent = result.find((item)=> item.name == )




        }
    }




    return result
}


// 渲染菜单的数据
export const menus = formatMenusFromRoutes(routes)
// 将路由表, 根据用户权限信息, 过滤的方法
// export const filterRoutesByAuth = (routes, checkedkeys) => {
//     return routes.filter((item) => checkedkeys.includes(item.name))
// }

export default routes
